/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.crab.service;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.common.utils.IdGen;
import com.kaaction.modules.crab.entity.Coupon;
import com.kaaction.modules.crab.entity.CouponProduct;
import com.kaaction.modules.sys.entity.User;
import com.kaaction.modules.sys.utils.UserUtils;
import com.kaaction.modules.crab.dao.CouponDao;
import com.kaaction.modules.crab.dao.CouponDetailDao;
import com.kaaction.modules.crab.dao.CouponProductDao;
import com.kaaction.modules.crab.dao.CrabProductDao;

/**
 * 兑换券管理Service
 * @author huanghaiming
 * @version 2017-07-19
 */
@Service
@Transactional(readOnly = true)
public class CouponService extends CrudService<CouponDao, Coupon> {
	
	@Autowired
	CouponProductDao couponProductDao;
	
	@Autowired
	CrabProductDao crabProductDao;
	
	@Autowired
	CouponDetailDao couponDetailDao;
	
	public Coupon get(String id) {
		return super.get(id);
	}
	
	public List<Coupon> findList(Coupon coupon) {
		return super.findList(coupon);
	}
	
	public Page<Coupon> findPage(Page<Coupon> page, Coupon coupon) {
		return super.findPage(page, coupon);
	}
	
	@Transactional(readOnly = false)
	public void save(Coupon coupon) {
		
		String uuid = IdGen.uuid();
		
		String productId = coupon.getProductId();
		if(StringUtils.isNotBlank(productId)){
			String[] pid = productId.split(",");
			CouponProduct couponProduct = new CouponProduct();
			for(String pi:pid){
				couponProduct.preInsert();
				couponProduct.setCouponId(uuid);
				couponProduct.setProductId(pi);
				couponProductDao.insert(couponProduct);
			}
			
			List<Map<String,Object>> list = crabProductDao.selectProductName(Arrays.asList(pid));
			String productName = "";
			if(list.size()>0){
				
				for(Map<String,Object>  map:list){
					productName+=map.get("name")+",";
				}
				coupon.setProductName(productName.substring(0, productName.length()-1));
			}
		}
		
		User user = UserUtils.getUser();
		Date date = new Date();
		
		List<Map<String,Object>> list = Lists.newArrayList();
		Map<String,Object> map = null;
		
		
		String cardNumRule= coupon.getCardNumRule();
		int  cNum = coupon.getNum();
		String rule = "";
		
		if(cNum>0){
			
			int num= cNum+1;
			
			for(int i=1;i<num;i++){
				map = new HashMap<String,Object>();
				map.put("id", IdGen.uuid());
				map.put("couponId", uuid);
				String cardNum= cardNumRule+String.format("%4d", i).replace(" ", "0");//补足0
				if(i==1){
					rule=cardNum;
				}
				map.put("cardNum", cardNum);
				Random random = new Random();
				int rannum = (int) (random.nextDouble() * (999999 - 100000 + 1)) + 100000;// 获取6位随机数
				map.put("password", rannum);
				map.put("activatedState", "0");
				map.put("useState", "0");
				map.put("createBy", user.getId());
				map.put("createDate", date);
				map.put("updateBy", user.getId());
				map.put("updateDate", date);
				map.put("delflag", "0");
				list.add(map);
				if(i==num-1){
					rule+="-"+cardNum;
				}
			}
			couponDetailDao.insertCouponDetail(list);
		}else{
			rule=cardNumRule;
		}
		
		if (StringUtils.isNotBlank(user.getId())){
			coupon.setUpdateBy(user);
			coupon.setCreateBy(user);
		}
		coupon.setCardNumRule(rule);
		coupon.setUpdateDate(date);
		coupon.setCreateDate(date);
		coupon.setId(uuid);
		super.dao.insert(coupon);
		
	}
	
	@Transactional(readOnly = false)
	public void delete(Coupon coupon) {
		super.delete(coupon);
		couponDetailDao.deleteCoupon(coupon.getId());
		couponProductDao.deleteCoupon(coupon.getId());
	}

	public void updateCoupon(String couponId) {
		super.dao.updateCoupon(couponId);
	}
	
}